package com.easyfun.healthmagicbox.widget;

import com.easyfun.healthmagicbox.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 
 * @author MJ
 */
public class DialogBuilder {
	Dialog dialog;
	Context context;
	public int dialogId;

	public DialogBuilder(Context context, int dialogId) {
		Dialog dialog = new Dialog(context, R.style.Dialog);
		dialog.setContentView(R.layout.dialog);
		
		dialog.getWindow().getAttributes().width = LayoutParams.WRAP_CONTENT;
		dialog.getWindow().getAttributes().height = LayoutParams.WRAP_CONTENT;
		this.dialog = dialog;
		this.context = context;
		this.dialogId = dialogId;
		dialog.setOwnerActivity((Activity) this.context);
	}

	public DialogBuilder(Context context) {
		this(context, 0);
	}

	public Dialog getDialog() {
		return dialog;
	}

	/**
	 *
	 * 
	 * @param title
	 * 
	 */
	public DialogBuilder setTitle(Object title) {
		
		TextView titleView = getView(R.id.title);
		titleView.setText(parseParam(title));

		return this;
	}

	/**
	 * 
	 */
	public DialogBuilder setMessage(Object message) {
		TextView messageView = getView(R.id.message);
		messageView.setText(parseParam(message));

		return this;
	}

	Button left;

	public DialogBuilder setButtons(Object leftBtn, Object rightBtn, final OnDialogButtonClickListener listener) {

		Button left = getView(R.id.left);
		left.setText(parseParam(leftBtn));

		left.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dialog.dismiss();

				if (listener != null) { 
					listener.onDialogButtonClick(context, DialogBuilder.this, dialog, dialogId, OnDialogButtonClickListener.BUTTON_LEFT);
				}
			}
		});
		this.left = left;

		Button right = getView(R.id.right);
		right.setText(parseParam(rightBtn));

		right.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dialog.dismiss();

				if (listener != null) { 
					listener.onDialogButtonClick(context, DialogBuilder.this, dialog, dialogId, OnDialogButtonClickListener.BUTTON_RIGHT);
				}
			}
		});
		return this;
	}

	/**
	 *
	 * 
	 * @param id
	 *            
	 * @return 
	 */
	@SuppressWarnings("unchecked")
	public <T extends View> T getView(int id) {
		return (T) dialog.findViewById(id);
	}

	/**
	 * 
	 * 
	 * @param param
	 *            
	 * @return 
	 */
	private String parseParam(Object param) {
		if (param instanceof Integer) {
			return context.getString((Integer) param);
		} else if (param instanceof String) {
			return param.toString();
		}
		return null;
	}

	/**
	 * 
	 * 
	 * @return
	 */
	public Dialog create() {
		if (left == null) { 
			
			ViewGroup btnsLayout = getView(R.id.btns_layout);

			ViewGroup root = getView(R.id.root);
		
			// ViewGroup root = (ViewGroup) btnsLayout.getParent();
			root.removeView(btnsLayout);
		}
		return dialog;
	}

	public interface OnDialogButtonClickListener {
		public static final int BUTTON_LEFT = 0;
		public static final int BUTTON_RIGHT = 1;

		public void onDialogButtonClick(Context context, DialogBuilder builder, Dialog dialog, int dialogId, int which);
	}

	/**
	 * 
	 */
	public DialogBuilder setView(View view) {
		// 1.
		ViewGroup messageLayout = getView(R.id.message_layout);
		// 2.
		messageLayout.removeAllViews();
		// 3.
		messageLayout.addView(view);
		return this;
	}

	/**
 	 *
	 * @param layout
	 * @return
	 */
	public DialogBuilder setView(int layout) {
		ViewGroup parent = getView(R.id.message_layout);
		View view = LayoutInflater.from(context).inflate(layout, parent, false);
		return setView(view);
	}
	
	public DialogBuilder setCustomizedContent(Object leftBtn, Object rightBtn, int viewid, final OnDialogButtonClickListener listener) {
		// 1.
		setView(viewid);

		Button left = getView(R.id.left);
		left.setText(parseParam(leftBtn));

		left.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dialog.dismiss();

				if (listener != null) { 
					listener.onDialogButtonClick(context, DialogBuilder.this, dialog, dialogId, OnDialogButtonClickListener.BUTTON_LEFT);
					
				}
			}
		});
		this.left = left;

		Button right = getView(R.id.right);
		right.setText(parseParam(rightBtn));

		right.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dialog.dismiss();

				if (listener != null) { 
					listener.onDialogButtonClick(context, DialogBuilder.this, dialog, dialogId, OnDialogButtonClickListener.BUTTON_RIGHT);
				}
			}
		});
		return this;
	}
	
	
	public DialogBuilder setItems(String[] items, final OnDialogItemClickListener listener) {
		// 1.
		setView(R.layout.dialog_items);

		// 2.
		ListView listView = getView(android.R.id.list);
		// item_text_view
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, R.layout.dialog_items_text_view, items);
		// new ArrayAdapter<T>(context, R.layout.item_text_view, R.id.text_view,
		// items)
		// listView.setAdapter(new SImple)
		listView.setAdapter(adapter);

		// 3.
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				dialog.dismiss();

				if (listener != null) {
					listener.onDialogItemClick(context, DialogBuilder.this, dialog, position);
				}
			}
		});
		return this;
	}

	public DialogBuilder setItems(int arrayId, OnDialogItemClickListener listener) {
		String[] items = context.getResources().getStringArray(arrayId);
		return setItems(items, listener);
	}

	public interface OnDialogItemClickListener {
		public void onDialogItemClick(Context context, DialogBuilder builder, Dialog dialog, int position);

	}
}
